// voiceInput.js - 语音输入处理模块

const VoiceInput = {
    // 属性
    data: {
        recognition: null,
        isListening: false,
        interimResult: '',
        finalResult: '',
        supportVoiceInput: 'webkitSpeechRecognition' in window || 'SpeechRecognition' in window,
        onResultCallback: null,
        onEndCallback: null
    },

    // 初始化
    init() {
        if (!this.data.supportVoiceInput) {
            //console.warn("当前浏览器不支持语音识别功能");
            return this;
        }

        // 确保初始状态是关闭的
        this.data.isListening = false;
        
        // 初始化语音识别
        const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
        this.data.recognition = new SpeechRecognition();
        
        // 配置语音识别
        this.data.recognition.continuous = false;
        this.data.recognition.interimResults = true;
        this.data.recognition.lang = 'zh-CN'; // 设置为中文

        // 确保初始状态为false
        this.data.isListening = false;
        
        // 监听事件
        this.data.recognition.onresult = (event) => {
            let interimTranscript = '';
            let finalTranscript = '';
            
            for (let i = event.resultIndex; i < event.results.length; i++) {
                const transcript = event.results[i][0].transcript;
                if (event.results[i].isFinal) {
                    finalTranscript += transcript;
                } else {
                    interimTranscript += transcript;
                }
            }
            
            this.data.interimResult = interimTranscript;
            if (finalTranscript) {
                this.data.finalResult += finalTranscript;
            }
            
            if (this.data.onResultCallback) {
                this.data.onResultCallback(this.data.interimResult, this.data.finalResult);
            }
        };
        
        this.data.recognition.onend = () => {
            this.data.isListening = false;
            
            if (this.data.onEndCallback) {
                this.data.onEndCallback(this.data.finalResult);
            }
            
            this.data.interimResult = '';
            this.data.finalResult = '';
        };
        
        return this;
    },
    
    // 开始语音输入
    startListening(onResultCallback, onEndCallback) {
        if (!this.data.supportVoiceInput) {
            //console.warn("浏览器不支持语音识别");
            return false;
        }
        
        try {
            this.data.finalResult = '';
            this.data.interimResult = '';
            this.data.onResultCallback = onResultCallback;
            this.data.onEndCallback = onEndCallback;
            
            this.data.recognition.start();
            this.data.isListening = true;
            
            //.log("开始语音识别");
            return true;
        } catch (error) {
            //console.error("启动语音识别失败:", error);
            return false;
        }
    },
    
    // 停止语音输入
    stopListening() {
        if (this.data.isListening) {
            try {
                this.data.recognition.stop();
                return true;
            } catch (error) {
                //console.error("停止语音识别失败:", error);
                return false;
            }
        }
        return false;
    },
    
    // 检测是否支持语音输入
    isSupported() {
        return this.data.supportVoiceInput;
    },
    
    // 当前是否正在监听
    isActive() {
        return this.data.isListening;
    }
};

// 导出模块
window.VoiceInput = VoiceInput;